package com.uwaterloo.interviewme;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.uwaterloo.interviewme.am.ApplicationManager;
import com.uwaterloo.interviewme.exception.ServerException;

/**
 * The Class {@link Settings} displays a setting view where users can set their syncing interview.
 */
public class Settings extends Activity {

	/** The Constant sync_arry that specifies all supported syncing interval. */
	private static final int[] sync_arry = new int[] { 10, 20, 30 };

	/** The application manager. */
	private ApplicationManager am;

	/** The selected interval. */
	private int selectedInterval;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		am = ApplicationManager.getInstance();

		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this, R.array.sync_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new SettingsOnItemSelectedListener());

		Button loginButton = (Button) findViewById(R.id.settingSave);
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					am.setInterval(selectedInterval);
					Toast.makeText(v.getContext(), "Syncing interval saved.", Toast.LENGTH_LONG).show();
				} catch (ServerException e) {
					Toast.makeText(v.getContext(), "Server unavailable. Saved failed.", Toast.LENGTH_LONG).show();
				}
				
			}
		});
	}

	/**
	 * The listener interface for receiving settingsOnItemSelected events. The class that is
	 * interested in processing a settingsOnItemSelected event implements this interface, and the
	 * object created with that class is registered with a component using the component's
	 * <code>addSettingsOnItemSelectedListener<code> method. When
	 * the settingsOnItemSelected event occurs, that object's appropriate
	 * method is invoked.
	 * 
	 * @see SettingsOnItemSelectedEvent
	 */
	private class SettingsOnItemSelectedListener implements OnItemSelectedListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView
		 * , android.view.View, int, long)
		 */
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			selectedInterval = sync_arry[pos];
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.
		 * AdapterView)
		 */
		@SuppressWarnings("unchecked")
		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

}
